//
//  LGTRWZPostInputCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/22.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZPostInputCell: UITableViewCell {

    var titleLabel:UILabel?
    var line:UIImageView?
    var textField:LGTInputTextView?
    var returnStrBlock:ClickCellStrBlock?  //返回给j交互视图
    var textChangeBlock:ClickReturnStrBlock?  //键盘文字变化回掉处理
    private var _model:Dictionary<String, Any>?
    var path:IndexPath?
    var model:Dictionary<String, Any>{
        get{
            return _model ?? Dictionary.init()
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    func setData(){
        self.titleLabel?.text = self.model["title"] as? String ?? ""
        let limit:Int = self.model["limit"] as? Int ?? -1
        
        //        self.textField?.updateInfo(fontSize: SizeFitFloat(15), textColor: TextColor, limitCount: 10, placeHoder: "test")
        self.textField?.updateInfo(fontSize: SizeFitFloat(15), textColor: TextColor, limitCount: limit, placeHoder: self.model["placehoder"] as? String ?? "")
        self.textField?.textView.keyboardType = self.model["keyboard"]  as? UIKeyboardType ?? UIKeyboardType.default
        
        let textFieldText:String = self.model["textFieldText"] as? String ?? ""
        self.textField?.text = textFieldText
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.titleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15))
        self.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints({ (make) in
            make.top.equalTo(SizeFit(19))
            make.left.equalTo(SizeFit(14))
            make.height.equalTo(SizeFitFloat(15))
        })
        
        let button:UIButton = UIButton.init()
        self.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFit(200))
            make.left.equalTo(SizeFit(94))
            make.bottom.equalTo(0)
            make.top.equalTo(SizeFit(0))
        }
        button.addTarget(self, action: #selector(gotoAction), for: UIControl.Event.touchUpInside)
        
        self.textField = LGTInputTextView.init(frame: CGRect.zero)
//        self.textField?.updateInfo(fontSize: SizeFitFloat(15), textColor: TextColor, limitCount: 10, placeHoder: "test")
        self.addSubview(self.textField!)
        self.textField?.snp.makeConstraints({ (make) in
            make.width.equalTo(SizeFit(200))
            make.height.equalTo(SizeFit(20))
            make.left.equalTo(SizeFit(94))
            make.top.equalTo(SizeFit(19))
        })
        
        self.line = LGTViewCreateUtil.share.createLineView()
        self.addSubview(self.line!)
        self.line?.snp.makeConstraints({ (make) in
            make.top.equalTo(SizeFit(50))
            make.height.equalTo(1)
            make.left.equalTo(SizeFitFloat(13))
            make.right.equalTo(SizeFitFloat(-11))
            make.bottom.equalTo(0)
        })
        
        //值变化回掉
        weak  var weakSelf = self
        self.textChangeBlock = {
            (text:String) -> Void in
            weakSelf?.setTextLimit(text: text)
            if weakSelf?.returnStrBlock != nil{
                weakSelf?.returnStrBlock!(text, weakSelf!.path!)
            }
        }
        self.textField?.returnReturnStrBlock = self.textChangeBlock
        
//        //监听键盘弹出
//        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
//
//        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHiden), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    //移除通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // 监听键盘高度变化
    @objc private func keyboardWillHiden(node : Notification){
     print("hidden")
        
        if self.textField?.textView.isFirstResponder == true{
            self.textField?.snp.updateConstraints({ (make) in
                make.height.equalTo(SizeFit(30))
            })
            
            
            self.line?.snp.updateConstraints({ (make) in
                make.top.equalTo(SizeFit(42))
            })
        }
    }

   // 监听键盘高度变化
    @objc private func keyboardWillShow(node : Notification){
//        //1.获取动画执行的时间
//        let duration =  node.userInfo!["UIKeyboardAnimationDurationUserInfoKey"] as! Double
//        //2. 获取键盘最终的Y值
//        let endFrame = (node.userInfo!["UIKeyboardFrameEndUserInfoKey"] as! NSValue).cgRectValue
//        let y = endFrame.origin.y
        if self.textField?.textView.isFirstResponder == true{
            self.textField?.snp.updateConstraints({ (make) in
                make.height.equalTo(SizeFit(100))
            })
            
            self.line?.snp.updateConstraints({ (make) in
                make.top.equalTo(SizeFit(112))
            })
        }
    }

    //限制
    func setTextLimit(text:String){
        //最低单价 和 任务数量
        let isTaskCount:Bool = self.model["isTaskCount"] as? Bool ?? false
        let isTaskPrice:Bool = self.model["isTaskPrice"] as? Bool ?? false
        let type:PostNameModel? = self.model["type"] as? PostNameModel
        if type == nil{
            return
        }
        
        if isTaskCount == true && text.count > 0{
            
            let count:Int = Int(text) ?? 0
            if count < type!.min_count ?? 0{
                self.textField?.textView.text = "\(type?.min_count ?? 0)"
            }
            
        }else if isTaskPrice == true && text.count > 0{
            let count:Double = Double(text) ?? 0
            if count < Double(type!.min_price ?? 0) / 100.0{
                self.textField?.textView.text = "\(Double(type?.min_price ?? 0)/100.0)"
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func gotoAction(){
        self.textField?.becomeFirstResponder()
    }
}
